﻿package javase.socket.server;

import java.net.*;
import java.io.*;
import java.util.*;
/**
 * @author Dong
 *
 */
public class ChatServer1 {
	public static void main(String[] args) 
	{
		try
		{
			
			HashMap users=new HashMap();
			ServerSocket ss=new ServerSocket(6001);
			boolean sRunning=true;
			System.out.println("服务器已启动");
			while(sRunning)
			{
				Socket s=ss.accept();
				DataInputStream ips=new DataInputStream(s.getInputStream());
				DataOutputStream ops=new DataOutputStream(s.getOutputStream());
				String name=ips.readUTF();
				Sthread1 st=new Sthread1(s,users);
				users.put(name,st);
				new Thread(st).start();
				Set nameset=users.keySet();
		        Iterator it=nameset.iterator();
		        while(it.hasNext())
		        {
		        	String temp=(String)it.next();
		        	Sthread1 sr=(Sthread1)users.get(temp);
		      		sr.write("欢迎用户"+name+"来到JQB专用聊天室");
		      		String allnames=new String("#@#");
					Set allusers=users.keySet();
			      	Iterator i=allusers.iterator();
			      	while(i.hasNext())
			      	{
			      		String tmp=(String)i.next();
			      		allnames=allnames+tmp+"#";
			      	}
			      	System.out.println(allnames);
		      		sr.write(allnames);
				}
			}
			ss.close();
		}
		catch(Exception e)
		{e.printStackTrace();}
	}
}

class Sthread1 implements Runnable
{
	Socket s=null;
	HashMap users=new HashMap();
	DataInputStream ips=null;
	DataOutputStream ops=null;
	Sthread1(Socket s,HashMap users)
	{
		this.s=s;
		this.users=users;
		try
		{
			ips=new DataInputStream(s.getInputStream());
			ops=new DataOutputStream(s.getOutputStream());				
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public void run()
	{
		try
		{
			boolean sRunning=true;
			while(sRunning)
			{
				String strLine=ips.readUTF();
				if(strLine.substring(0,3).trim().equals("#a#"))//#a#hisname#dialog群聊
				{
					strLine=strLine.substring(3);
					Set allusers=users.keySet();
			      	Iterator i=allusers.iterator();
			      	String hisname=strLine.substring(0,strLine.indexOf("#"));
			      	while(i.hasNext())
			      	{
			      		String tmp=(String)i.next();
			      		Sthread1 sr=(Sthread1)users.get(tmp);
		      			sr.write(hisname+"说:"+strLine.substring(strLine.indexOf("#")+1));
			      	}
				}
				if(strLine.substring(0,3).trim().equals("#p#"))//#p#hisname#toname#dialog私聊
				{
					strLine=strLine.substring(3);
					String myname=strLine.substring(0,strLine.indexOf("#"));
					String toname=strLine.substring(strLine.indexOf("#")+1,strLine.indexOf("#",strLine.indexOf("#")+1));
					Set allusers=users.keySet();
			      	Iterator i=allusers.iterator();
			      	while(i.hasNext())
			      	{
			      		String tmp=(String)i.next();
			      		if(tmp.equals(toname))
			      		{
			      			Sthread1 sr=(Sthread1)users.get(tmp);
			      			sr.write(myname+"对您说:"+strLine.substring(strLine.indexOf("#",strLine.indexOf("#")+1)+1));
			      		}
			      		else if(tmp.equals(myname))
			      		{
			      			Sthread1 sr=(Sthread1)users.get(tmp);
			      			sr.write("你对"+myname+"说:"+strLine.substring(strLine.indexOf("#",strLine.indexOf("#")+1)+1));
			      		}
			      	}
				}
				else if(strLine.substring(0,3).trim().equals("#q#"))//#q#myname用户离开
				{
					String myname=strLine.substring(3);
					Set allusers=users.keySet();
					Iterator i=allusers.iterator();
					while(i.hasNext())
					{
						String tmp=(String)i.next();
						Sthread1 sr=(Sthread1)users.get(tmp);
						if(tmp.equals(myname))
						{	
							sr.closewrite("#q#"+myname+"#你离开了聊天室");
						}
						else
						{
							sr.write("#q#"+myname+"#离开了聊天室");
						}
					}
					users.remove(myname);
					return;
				}
			}
		}
		catch(Exception e)
		{e.printStackTrace();}
	}
    public void write(String msg)
    {
    	try
    	{
    		ops.writeUTF(msg);
    	}
    	catch(Exception e)
    	{e.printStackTrace();}
    }
    public void closewrite(String msg)
    {
    	try
    	{
    		ops.writeUTF(msg);
    		ips.close();
			ops.close();
			s.close();
    	}
    	catch(Exception e)
    	{e.printStackTrace();}
    }
}
